package com.practice.xml.sax;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.practice.xml.Company;
import com.practice.xml.CompanyDefinition;
import com.practice.xml.EmployeeDefinition;
import com.practice.xml.XmlProcessor;

/**
 * Creates and parses XML document by SAX.
 * @author Leonardo
 *
 */
public class SaxXmlProcessor implements XmlProcessor {

	@Override
	public void createXml(Company company, String filePath) {
		
	}

	@Override
	public Company parseXml(String filePath) {
		SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
		try {
			SAXParser saxParser = saxParserFactory.newSAXParser();
			InputStream is = new FileInputStream(filePath);
			parseUTF8(saxParser, is);
		} catch (ParserConfigurationException ex) {
			System.out.println(ex.getMessage());
			System.out.println(ex.getStackTrace());
		} catch (SAXException ex) {
			System.out.println(ex.getMessage());
			System.out.println(ex.getStackTrace());
		} catch (FileNotFoundException ex) {
			System.out.println(ex.getMessage());
			System.out.println(ex.getStackTrace());
		} catch (IOException ex) {
			System.out.println(ex.getMessage());
			System.out.println(ex.getStackTrace());
		}
		return null;
	}
	
	/*private void parse(SAXParser saxParser, InputStream is) 
			throws SAXException, IOException {
		saxParser.parse(is, new MySAXHandler());
	}*/
	
	private void parseUTF8(SAXParser saxParser, InputStream is) 
			throws SAXException, IOException {
		try {
			Reader reader = new InputStreamReader(is, "utf-8");
			InputSource source = new InputSource(reader);
			saxParser.parse(source, new MySAXHandler());
		} catch (UnsupportedEncodingException ex) {
			System.err.println(ex.getMessage());
			System.err.println(ex.getStackTrace());
		}
		
	}
}

class MySAXHandler extends DefaultHandler {
	boolean hasAttribute = false;
	Attributes attributes = null;
	
	public void startDocument() throws SAXException {
		System.out.println("Document started.");
	}
	
	public void endDocument() throws SAXException {
		System.out.println("Document ended.");
	}
	
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if (qName.equals(CompanyDefinition.NAME)) {
			System.out.println("Element " + qName);
		}
		
		if (qName.equals(CompanyDefinition.ELEMENT_EMPLOYEES)) {
			return;
		}
		
		if (qName.equals(EmployeeDefinition.NAME)) {
			System.out.println("Element " + qName);
		}
		
		if (attributes.getLength() > 0) {
			this.attributes = attributes;
			this.hasAttribute = true;
			

			for (int i = 0; i < attributes.getLength(); i++) {
				System.out.println("Attribute " + attributes.getQName(i) + 
						"(" + attributes.getValue(i) + ")");
			}
		}
	}
	
	public void endElement(String uri, String localName, String qName)
		throws SAXException {
		if (hasAttribute && (attributes != null)) {
			for (int i = 0; i < attributes.getLength(); i++) {
				System.out.println("Attribute " + attributes.getQName(i) + 
						"(" + attributes.getValue(i) + ")");
			}
		}
	}
	
	public void characters(char[] ch, int start, int length) 
		throws SAXException {
		String value = new String(ch, start, length);
		value = value.trim();
		if (!value.isEmpty()) {
			System.out.println("Value " + value);
		}
	}
}









